using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using FireStarterModeller.UI.DetailsView.WPF;
using Habanero.Util;
using NUnit.Framework;

namespace FireStarterModeller.Test.UI.DetailsView.WPF
{
    [TestFixture]
    public class TestRenameItemPopup
    {
        [Test]
        public void TestOKButtonClickedEvent()
        {
            CrossThreadRunner runner = new CrossThreadRunner();
            runner.RunInSTA
                (() =>
                 {
                     //---------------Set up test pack-------------------
                     RenameItemPopup popup = new RenameItemPopup();
                     Panel panel = (Panel) popup.Child;
                     Button button = GetOKButton(panel);
                     RoutedEventArgs clickEventArgs = new RoutedEventArgs(ButtonBase.ClickEvent);
                     bool eventRaised = false;
                     popup.OKButtonClicked += (sender, e) => eventRaised = true;

                     //---------------Execute Test ----------------------
                     button.RaiseEvent(clickEventArgs);
                     //---------------Test Result -----------------------
                     Assert.IsTrue(eventRaised);
                     //---------------Tear Down -------------------------      
                 });
        }

        [Test]
        public void TestInit()
        {
            CrossThreadRunner runner = new CrossThreadRunner();
            runner.RunInSTA
                (() =>
                 {
                     //---------------Set up test pack-------------------
                     RenameItemPopup popup = new RenameItemPopup();
                     Panel panel = (Panel) popup.Child;
                     const string itemType = "assembly";
                     const string itemName = "assembly1";
                     object itemToRename = "asdf";
                     //---------------Execute Test ----------------------
                     popup.Init(itemToRename, itemType, itemName);
                     string labelContent = GetLabelContent(panel);
                     //---------------Test Result -----------------------
                     Assert.AreEqual("Rename assembly assembly1 to...", labelContent);
                     Assert.AreEqual(itemName, popup.NewName);
                     Assert.AreSame(itemToRename, popup.ItemToRename);
                     //---------------Tear Down -------------------------      
                 });
        }

        private string GetLabelContent(Panel panel)
        {
            foreach (object child in panel.Children)
            {
                if (child is Label) return (string) ((Label) child).Content;
            }
            return null;
        }

        private Button GetOKButton(Panel panel)
        {
            foreach (object child in panel.Children)
            {
                if (child is Button) return (Button) child;
            }
            return null;
        }
    }
}